package qunchat;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;

public class ServerThread extends Thread {
	private Socket s;
	private List<Socket> ls;
	public ServerThread(Socket s,List<Socket> ls) {
		this.s=s;
		this.ls=ls; 		
	}
	public void run() {
			ObjectOutputStream out=null;
		//	StringBuilder sb=new StringBuilder();
		try {
			ObjectInputStream in=new ObjectInputStream(s.getInputStream());
			
		   while(true){	
			   String ch= (String) in.readObject();
			   // sb.append((String)ch);
			   //给在list中的客户端都分别发送消息
			   for (int i=0;i<ls.size();i++) {
				   out=new ObjectOutputStream(ls.get(i).getOutputStream());
				  out.writeObject(ch);
				   out.flush();
			   }
			   in=new ObjectInputStream(s.getInputStream());
			  // sb.delete(0,sb.length());
		   }			  
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
